//
//  BLHomeViewController.swift
//  BLLiveTelecast
//
//  Created by BigL055 on 16/5/20.
//  Copyright © 2016年 bigL.liveTelecast.com. All rights reserved.
//

import UIKit
import AFNetworking


class BLHomeViewController: UIViewController {
    
    private lazy var collectionView : UICollectionView = UICollectionView()
    
    //专栏视图
    private let columnTitleView = UIView()
    //专栏按钮数组
    private var titleBtnArray = [UIButton]()
    //上一个选中的专栏按钮
    private var titlePreBtn : UIButton?
    
    //视图数组
    private lazy var viewArray = [UIView]()
    
    private let collectionViewCellID = "homePlaceViewCell"
    
}



// MARK: - 主业务逻辑
extension BLHomeViewController{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.grayColor()
        //设置NavgationBar
        setupNavgationBar()
        //设置UI
        configureUI()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBarHidden = false
    }
}

// MARK:- 设置导航栏内容
extension BLHomeViewController{
    
    func setupNavgationBar(){
        
        self.navigationController?.navigationItem.title = "首页";
        
        //导航栏左侧按钮
        let leftBtn = UIBarButtonItem.bl_BarButtonItem(norImage: UIImage(named: "Image_launch_logo_72x30_"),highImage:UIImage(named: "Image_launch_logo_72x30_"),target: self,action: #selector(BLHomeViewController.leftBtnClick))
        
        navigationItem.leftBarButtonItem = leftBtn
        
        //导航栏右侧按钮
        let scanBtn = UIBarButtonItem.bl_BarButtonItem(norImage: UIImage(named: "Image_scan_22x22_"), highImage: UIImage(named: "Image_scan_click_22x22_"), target: self, action: #selector(BLHomeViewController.scanBtnClick))
        
        let searchBtn = UIBarButtonItem.bl_BarButtonItem(norImage: UIImage(named: "btn_search_22x22_"), highImage: UIImage(named: "btn_search_clicked_22x22_"), target: self, action: #selector(BLHomeViewController.searchBtnClick))
        
        let historyBtn = UIBarButtonItem.bl_BarButtonItem(norImage: UIImage(named: "image_my_history_26x26_"), highImage: UIImage(named: "Image_my_history_click_22x22_"), target: self, action: #selector(BLHomeViewController.historyBtnClick))
        
                
        navigationItem.rightBarButtonItems = [historyBtn,searchBtn,scanBtn]
        
    }
    
    
    
    func leftBtnClick() {
        print("回到主页")
    }
    
    func scanBtnClick() {
        print("扫描二维码")
        let scanVC = BLScanViewController()
        navigationController?.pushViewController(scanVC, animated: true)
        
    }
    func searchBtnClick() {
        print("搜索")
        let searchVC = BLSearchViewController()
        navigationController?.pushViewController(searchVC, animated: true)
    }
    func historyBtnClick() {
        print("我的历史")
    }
}

// MARK:- 配置UI
extension BLHomeViewController{
    
    func configureUI() {
        //专栏视图
        setupColumnTitleView()
        setupCollectionView()
    }
}

// MARK: - 配置专栏视图
extension BLHomeViewController{
    
    /// MAKE - 专栏视图
    func setupColumnTitleView() {
        view.addSubview(columnTitleView)
        columnTitleView.snp_makeConstraints { (make) in
            make.top.equalToSuperview().offset(BL_StatusBarAndNavigationBarHeight)
            make.right.equalToSuperview()
            make.left.equalToSuperview()
            make.height.equalTo(40)
        }
        
        let commendBtn = setupTitleSingleButtom("推荐", selector: #selector(titleBtnClick(_:)))
        let liveListBtn = setupTitleSingleButtom("直播列表", selector: #selector(titleBtnClick(_:)))
        
        columnTitleView.addSubview(commendBtn)
        columnTitleView.addSubview(liveListBtn)
        
        commendBtn.snp_makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.bottom.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
        }
        
        liveListBtn.snp_makeConstraints { (make) in
            make.top.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
        }
        
    }
    /// 专栏按钮点击事件
    func titleBtnClick(button : UIButton) {
        button.selected = true
        titlePreBtn?.selected = false
        titlePreBtn = button
        let indexPath = NSIndexPath(forItem: button.tag, inSection: 0)
        collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: true)
    }
    
    /// 返回单个专栏按钮
    func setupTitleSingleButtom(title : String,
                                selector : Selector) -> UIButton {
        let button = UIButton(type: UIButtonType.Custom)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Selected)
        button.addTarget(self, action: selector, forControlEvents: UIControlEvents.TouchUpInside)
        button.tag = titleBtnArray.count
        if titleBtnArray.count == 0 {
            button.selected = true
            titlePreBtn = button
        }
        titleBtnArray.append(button)
        return button
    }
}

// MARK: - 设置CollectionViewUI
extension BLHomeViewController{
    
    func setupCollectionView() {
        
        //添加推荐子控制器
        let commendVC = BLCommendViewController()
        addSingleChildViewController(commendVC)
        
        //添加斗鱼全部分类控制器
        let douYuLiveListVC = BLDouYuLiveListViewController()
        addSingleChildViewController(douYuLiveListVC)
        
        //添加 collectionView
        collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: BLCollectionViewFlowLayout())
        collectionView.backgroundColor = UIColor.clearColor()
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.bounces = false
        collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewCellID)
        view.addSubview(collectionView)
        collectionView.snp_makeConstraints { (make) in
            make.top.equalTo(columnTitleView.snp_bottom)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(-BL_TabbarHeight)
        }
    }
}

// MARK: - UICollectionView代理与数据源
extension BLHomeViewController : UICollectionViewDelegate,UICollectionViewDataSource{
    
    //添加单个控制器
    func addSingleChildViewController(viewController : UIViewController) {
        self.viewArray.append(viewController.view)
        self.addChildViewController(viewController)
    }
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
        
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewCellID, forIndexPath: indexPath)
        
        let view = viewArray[indexPath.item]
        view.frame = cell.contentView.bounds
        cell.contentView.addSubview(view)
        return cell
    }
    
    //TODO: 滚动问题
    func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        let offset = scrollView.contentOffset
        let titleBtnindex = Int(offset.x / (BL_SCREEN_WIDTH - 200))
        let btn = titleBtnArray[titleBtnindex]
        print(titleBtnindex,btn.titleLabel?.text)
        titleBtnClick(btn)
    }
}

